import { repeatName, getUserLogin, userVerify, loginOut, getAddUsers, getAllUsers, deleteById, changeById,getOneUsers } from "@/services/usersService"
// import PagerComp from '@/components/PagerComp'
export default {
    namespaced: true,
    state: {
        isLoading: false, //加载状态
        user: null, //存储用户数据    
        users: [], //显示所有用户数据
        total: null, //总条数
    },
    mutations: { //同步操作
        setIsLoading(state, payload) {
            state.isLoading = payload;
        },
        setUser(state, payload) { //payload是形参 调用时传值
            state.user = payload
        },
        setUsers(state, payload) {
            state.users = payload
        },
        //得到总条数
        setTotal(state, payload) {
            state.total = payload
        },
        // setNewUsers(state, payload) {  //删除事件
        //     state.users = state.users.filter(item=>item._id!=payload);
        // }
    },
    actions: {
        async fetchUserData(context, payload) { //登录
            context.commit("setIsLoading", true)
            let { name, pass } = payload;
            console.log(name, pass);
            let resp = await getUserLogin(name, pass);
            context.commit("setUser", resp.data);
            console.log("setUser", resp.data); //得到用户数据数组
            context.commit("setIsLoading", false)
            return resp.data
        },
        async verify(context) { //验证token
            context.commit("setIsLoading", true);
            let resp = await userVerify();
            context.commit("setUser", resp.data);
            console.log("setUser", resp.data);
            context.commit("setIsLoading", false);
            return resp.data;
        },
        async repeat(context, payload) { //验证注册名称是否重复
            context.commit("setIsLoading", true);
            let { name } = payload;
            console.log("name", name);
            let resp = await repeatName(name);
            console.log("resp", resp);
            context.commit("setIsLoading", false)
            return resp;
        },
        async getAllUsers(context, payload) { //查询全部
            if (payload) {
                context.commit("setIsLoading", true);
                let { page, pageSize } = payload;
                let resp = await getAllUsers(page, pageSize);
                console.log("resp", resp);
                context.commit("setUsers", resp.rows);
                context.commit("setTotal", resp.total);
                console.log("total", resp.total);
                context.commit("setIsLoading", false)
                return resp;
            } else {
                context.commit("setIsLoading", true);
                let resp = await getAllUsers();
                console.log("resp", resp);
                context.commit("setUsers", resp.rows);
                context.commit("setTotal", resp.total);
                console.log("total", resp.total);
                context.commit("setIsLoading", false)
                return resp;
            }


        },
        async getOneUser(context, payload) { //查询一个
            context.commit("setIsLoading", true);
            let { type, value } = payload;
            console.log("payload", payload);
            let resp = await getOneUsers(type, value); //传入实参
            console.log("resp", resp);
            context.commit("setUsers", resp.rows); //更新数组
            context.commit("setTotal", resp.total);
            context.commit("setIsLoading", false)
        },
        async getUsers(context, payload) { //新增用户
            context.commit("setIsLoading", true);
            await getAddUsers(payload);
            console.log("add", payload);
            context.commit("setIsLoading", false)

        },
        async deleteOne(context, payload) { //删除一个
            context.commit("setIsLoading", true);
            await deleteById(payload); //删除数据库里的数据
            console.log("payload", payload);
            // context.commit("setNewUsers", payload); //删除页面数组里面的数据
            context.commit("setIsLoading", false)
        },
        async changeOne(context, payload) { //修改一个
            context.commit("setIsLoading", true);
            await changeById(payload);
            console.log("changeById", payload);
            context.commit("setIsLoading", false)
            // let {  _id,
            //     username,
            //     password,
            //     telephone,
            //     email,
            //     picture,
            //     privilege,
            //     createTime, }= payload;
            // await changeById( _id,
            //     username,
            //     password,
            //     telephone,
            //     email,
            //     picture,
            //     privilege,
            //     createTime, );

        },
        loginOut(context) { //注销
            loginOut(); //调用loginOut方法删除token
            context.commit("setUser", null); //把setUser赋值为空
        }
    }
}